package com.simin.mall.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.simin.base.easy.adapter.BaseTemplate
import com.simin.base.easy.adapter.BaseViewHolder
import com.simin.mall.R
import com.simin.mall.entity.HomeEntity

/**
 * 首页模板
 */
class HomeTemplate : BaseTemplate<HomeEntity, HomeTemplate.ViewHolder>() {

    override fun onCreateViewHolder(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(layoutInflater.inflate(R.layout.rv_home_layout, parent, false))
    }

    override fun getDataType(): Any {
        return HomeEntity::class.java
    }

    class ViewHolder : BaseViewHolder<HomeEntity> {
        var icon: ImageView? = null
        var title: TextView? = null
        var desc: TextView? = null
        var rvList: RecyclerView? = null

        constructor(view: View) : super(view)

        override fun initView(context: Context?) {
            icon = findViewById(R.id.item_icon_img)
            title = findViewById(R.id.item_title_tv)
            desc = findViewById(R.id.item_desc_tv)
            rvList = findViewById(R.id.rv_list)
        }

        override fun onBindData(data: HomeEntity?) {
            try {
                icon?.setImageResource(data?.img!!)
                title?.text = data?.title!!
                desc?.text = data?.desc!!
                rvList?.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
            }catch (e: Exception){
                e.printStackTrace()
            }
        }

    }
}